package io.xccit.hotelsystem.ui.hotel;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.textfield.TextInputEditText;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.databinding.FragmentHotelBinding;
import io.xccit.hotelsystem.ui.home.adapter.HotelAdapter;

/**
 * 酒店Fragment
 */
public class HotelFragment extends Fragment {

    private FragmentHotelBinding binding;
    private HotelViewModel viewModel;
    private Calendar startDate = Calendar.getInstance();
    private Calendar endDate = Calendar.getInstance();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        viewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @NonNull
            @Override
            public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
                return (T) new HotelViewModel(requireContext());
            }
        }).get(HotelViewModel.class);

        binding = FragmentHotelBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        
        initHotelTypeButtons();
        setupHotelList();

        
        // 初始化日期选择器
        TextInputEditText dateEditText = root.findViewById(R.id.date_input_layout).findViewById(R.id.text_input_edit_text);
        dateEditText.setOnClickListener(v -> showDateRangePicker());
        
        return root;
    }

    private void showDateRangePicker() {
        DatePickerDialog startDatePicker = new DatePickerDialog(getContext(), 
            (view, year, month, dayOfMonth) -> {
                startDate.set(year, month, dayOfMonth);
                showEndDatePicker();
            },
            startDate.get(Calendar.YEAR),
            startDate.get(Calendar.MONTH),
            startDate.get(Calendar.DAY_OF_MONTH)
        );
        startDatePicker.setTitle("选择入住日期");
        startDatePicker.show();
    }

    private void showEndDatePicker() {
        DatePickerDialog endDatePicker = new DatePickerDialog(getContext(),
            (view, year, month, dayOfMonth) -> {
                endDate.set(year, month, dayOfMonth);
                updateDateText();
            },
            endDate.get(Calendar.YEAR),
            endDate.get(Calendar.MONTH),
            endDate.get(Calendar.DAY_OF_MONTH)
        );
        endDatePicker.setTitle("选择离店日期");
        endDatePicker.show();
    }

    private void updateDateText() {
        String dateRange = dateFormat.format(startDate.getTime()) + " - " + dateFormat.format(endDate.getTime());
        TextInputEditText dateEditText = getView().findViewById(R.id.date_input_layout).findViewById(R.id.text_input_edit_text);
        dateEditText.setText(dateRange);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    
    private void initHotelTypeButtons() {
        binding.btnLuxury.setOnClickListener(v -> {
            viewModel.loadLuxuryHotels();
        });
        
        binding.btnBusiness.setOnClickListener(v -> {
            viewModel.loadBusinessHotels();
        });
        
        binding.btnEconomy.setOnClickListener(v -> {
            viewModel.loadEconomyHotels();
        });
        
        binding.btnQuick.setOnClickListener(v -> {
            viewModel.loadQuickHotels();
        });
    }
    
    private void setupHotelList() {
        // 设置横向布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(
            requireContext(), 
            LinearLayoutManager.HORIZONTAL, 
            false
        );
        binding.hotelList.setLayoutManager(layoutManager);
        
        // 设置适配器
        HotelAdapter adapter = new HotelAdapter(new ArrayList<>());
        binding.hotelList.setAdapter(adapter);
        
        // 观察数据变化
        viewModel.getHotels().observe(getViewLifecycleOwner(), hotels -> {
            adapter.setHotelList(hotels);
        });
        
        // 设置推荐更多列表
        LinearLayoutManager moreLayoutManager = new LinearLayoutManager(
            requireContext(),
            LinearLayoutManager.HORIZONTAL,
            false
        );
        binding.moreHotelList.setLayoutManager(moreLayoutManager);
        
        HotelAdapter moreAdapter = new HotelAdapter(new ArrayList<>());
        binding.moreHotelList.setAdapter(moreAdapter);
        
        // 观察推荐更多数据变化
        viewModel.getMoreHotels().observe(getViewLifecycleOwner(), hotels -> {
            moreAdapter.setHotelList(hotels);
        });
        
        // 默认加载豪华酒店和推荐更多
        viewModel.loadLuxuryHotels();
        viewModel.loadMoreHotels();
    }
}